#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created: Tue May 18 19:12:13 2010

from sys import argv
from PyQt4 import QtCore, QtGui

class FramePackageManager(QtGui.QFrame):
    def __init__(self, connection, parent = None):
        super(FramePackageManager, self).__init__(parent)
        """setup Interface"""
        self.setupUi(self)
        
        self.connection = connection
        self.packageManager = self.connection.root.Modules.PackageManagement.PackageManagement()
        
    def setupUi(self, package_manager_frame):
        package_manager_frame.setObjectName("package_manager_frame")
        package_manager_frame.resize(819, 580)
        package_manager_frame.setFrameShape(QtGui.QFrame.StyledPanel)
        package_manager_frame.setFrameShadow(QtGui.QFrame.Raised)
        self.gridLayout_5 = QtGui.QGridLayout(package_manager_frame)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.btnListInstalledPackages = QtGui.QPushButton(package_manager_frame)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("./icons/edit-find.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnListInstalledPackages.setIcon(icon)
        self.btnListInstalledPackages.setObjectName("btnListInstalledPackages")
        self.gridLayout.addWidget(self.btnListInstalledPackages, 0, 0, 1, 1)
        self.lstPackageList = QtGui.QListWidget(package_manager_frame)
        self.lstPackageList.setObjectName("lstPackageList")
        self.lstPackageList.setFixedSize(500, 580)
        self.gridLayout.addWidget(self.lstPackageList, 1, 0, 1, 1)
        self.gridLayout_5.addLayout(self.gridLayout, 0, 0, 3, 1)
        self.groupBox_25 = QtGui.QGroupBox(package_manager_frame)
        self.groupBox_25.setObjectName("groupBox_25")
        self.gridLayout_2 = QtGui.QGridLayout(self.groupBox_25)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.radioUseRPM = QtGui.QRadioButton(self.groupBox_25)
        self.radioUseRPM.setObjectName("radioUseRPM")
        self.gridLayout_2.addWidget(self.radioUseRPM, 0, 0, 1, 2)
        self.txtRPMPath = QtGui.QLineEdit(self.groupBox_25)
        self.txtRPMPath.setObjectName("txtRPMPath")
        self.gridLayout_2.addWidget(self.txtRPMPath, 0, 2, 1, 2)
        self.radioUseYUM = QtGui.QRadioButton(self.groupBox_25)
        self.radioUseYUM.setObjectName("radioUseYUM")
        self.gridLayout_2.addWidget(self.radioUseYUM, 1, 0, 1, 2)
        self.txtYUMPackageName = QtGui.QLineEdit(self.groupBox_25)
        self.txtYUMPackageName.setObjectName("txtYUMPackageName")
        self.gridLayout_2.addWidget(self.txtYUMPackageName, 1, 2, 1, 2)
        self.btnInstall = QtGui.QPushButton(self.groupBox_25)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("./icons/go-down.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnInstall.setIcon(icon1)
        self.btnInstall.setObjectName("btnInstall")
        self.gridLayout_2.addWidget(self.btnInstall, 2, 2, 1, 2)
        self.gridLayout_5.addWidget(self.groupBox_25, 0, 1, 1, 1)
        self.groupBox_24 = QtGui.QGroupBox(package_manager_frame)
        self.groupBox_24.setObjectName("groupBox_24")
        self.gridLayout_3 = QtGui.QGridLayout(self.groupBox_24)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.radioUninstallBySelection = QtGui.QRadioButton(self.groupBox_24)
        self.radioUninstallBySelection.setObjectName("radioUninstallBySelection")
        self.gridLayout_3.addWidget(self.radioUninstallBySelection, 0, 0, 1, 2)
        self.radioUninstallByName = QtGui.QRadioButton(self.groupBox_24)
        self.radioUninstallByName.setObjectName("radioUninstallByName")
        self.gridLayout_3.addWidget(self.radioUninstallByName, 1, 0, 1, 1)
        self.txtUninstallByName = QtGui.QLineEdit(self.groupBox_24)
        self.txtUninstallByName.setObjectName("txtUninstallByName")
        self.gridLayout_3.addWidget(self.txtUninstallByName, 1, 1, 1, 2)
        self.btnUninstall = QtGui.QPushButton(self.groupBox_24)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("./icons/go-up.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnUninstall.setIcon(icon2)
        self.btnUninstall.setObjectName("btnUninstall")
        self.gridLayout_3.addWidget(self.btnUninstall, 2, 2, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_24, 1, 1, 1, 1)
        self.groupBox_26 = QtGui.QGroupBox(package_manager_frame)
        self.groupBox_26.setObjectName("groupBox_26")
        self.gridLayout_4 = QtGui.QGridLayout(self.groupBox_26)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.btnSearch = QtGui.QPushButton(self.groupBox_26)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("./icons/page-zoom.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnSearch.setIcon(icon3)
        self.btnSearch.setObjectName("btnSearch")
        self.gridLayout_4.addWidget(self.btnSearch, 0, 1, 1, 1)
        self.txtSearch = QtGui.QLineEdit(self.groupBox_26)
        self.txtSearch.setObjectName("txtSearch")
        self.gridLayout_4.addWidget(self.txtSearch, 0, 0, 1, 1)
        self.txtPackageInfo = QtGui.QTextEdit(self.groupBox_26)
        self.txtPackageInfo.setObjectName("txtPackageInfo")
        self.gridLayout_4.addWidget(self.txtPackageInfo, 2, 0, 1, 2)
        self.label_46 = QtGui.QLabel(self.groupBox_26)
        self.label_46.setObjectName("label_46")
        self.gridLayout_4.addWidget(self.label_46, 1, 0, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_26, 2, 1, 1, 1)
        
        ################################ Signals connection ###############################
        
        self.connect(self.btnListInstalledPackages, QtCore.SIGNAL('clicked()'), self.listPackages)
        self.connect(self.lstPackageList, QtCore.SIGNAL('itemSelectionChanged()'), self.showInfo)
        self.connect(self.btnInstall, QtCore.SIGNAL('clicked()'), self.installPackage)
        self.connect(self.btnUninstall, QtCore.SIGNAL('clicked()'), self.uninstallPackage)
        self.connect(self.btnSearch, QtCore.SIGNAL('clicked()'), self.search)
        
        ###################################################################################
        
        self.retranslateUi(package_manager_frame)
        QtCore.QMetaObject.connectSlotsByName(package_manager_frame)

    def retranslateUi(self, package_manager_frame):
        package_manager_frame.setWindowTitle(QtGui.QApplication.translate("package_manager_frame", "Frame", None, QtGui.QApplication.UnicodeUTF8))
        self.btnListInstalledPackages.setText(QtGui.QApplication.translate("package_manager_frame", "Show installed packages", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_25.setTitle(QtGui.QApplication.translate("package_manager_frame", "Install new Package", None, QtGui.QApplication.UnicodeUTF8))
        self.radioUseRPM.setText(QtGui.QApplication.translate("package_manager_frame", "RPM file location", None, QtGui.QApplication.UnicodeUTF8))
        self.radioUseYUM.setText(QtGui.QApplication.translate("package_manager_frame", "Package Name (YUM)", None, QtGui.QApplication.UnicodeUTF8))
        self.btnInstall.setText(QtGui.QApplication.translate("package_manager_frame", "Install..", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_24.setTitle(QtGui.QApplication.translate("package_manager_frame", "Remove Package", None, QtGui.QApplication.UnicodeUTF8))
        self.radioUninstallBySelection.setText(QtGui.QApplication.translate("package_manager_frame", "Uninstall selected Package", None, QtGui.QApplication.UnicodeUTF8))
        self.radioUninstallByName.setText(QtGui.QApplication.translate("package_manager_frame", "Uninstall package by name", None, QtGui.QApplication.UnicodeUTF8))
        self.btnUninstall.setText(QtGui.QApplication.translate("package_manager_frame", "Uninstall..", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_26.setTitle(QtGui.QApplication.translate("package_manager_frame", " Search for package", None, QtGui.QApplication.UnicodeUTF8))
        self.btnSearch.setText(QtGui.QApplication.translate("package_manager_frame", "Search installed packages", None, QtGui.QApplication.UnicodeUTF8))
        self.label_46.setText(QtGui.QApplication.translate("package_manager_frame", "Package information:", None, QtGui.QApplication.UnicodeUTF8))
        ######### set tool tips #############
        self.txtRPMPath.setToolTip(QtGui.QApplication.translate("Form", "pass the url or the path of the rpm package", None, QtGui.QApplication.UnicodeUTF8))
        self.txtYUMPackageName.setToolTip(QtGui.QApplication.translate("Form", "pass the name of the package you want to install", None, QtGui.QApplication.UnicodeUTF8))
        self.txtUninstallByName.setToolTip(QtGui.QApplication.translate("Form", "pass the name of the package you want to uninstall", None, QtGui.QApplication.UnicodeUTF8))
        self.txtSearch.setToolTip(QtGui.QApplication.translate("Form", "search for specefic package by name", None, QtGui.QApplication.UnicodeUTF8))
        

    def listPackages(self):
        packageList = self.packageManager.listAll()
        self.lstPackageList.clear()
        self.txtPackageInfo.clear()
        for package in packageList :
            self.lstPackageList.addItem(package)
    
    def showInfo(self):
        selectedPackage = self.lstPackageList.currentItem().text()
        packageInfo = self.packageManager.packageInfo(str(selectedPackage))
        self.txtPackageInfo.setText(packageInfo)
    
    def installPackage(self):
        packagePath = str(self.txtRPMPath.text())
        packageName = str(self.txtYUMPackageName.text())
        
        if len(packagePath) > 0 and self.radioUseRPM.isChecked():
            self.txtRPMPath.clear()
            if self.packageManager.install(path = packagePath) :
                QtGui.QMessageBox.information(self,'Installation succeeded','The Package was successfully installed.')
            else :
                QtGui.QMessageBox.critical(self, 'Installation Failed', 'Failed to install this package.')
        
        elif len(packageName) > 0 and self.radioUseYUM.isChecked() :
            self.txtYUMPackageName.clear()
            if self.packageManager.install(name = packageName) :
                QtGui.QMessageBox.information(self,'Installation succeeded','The Package was successfully installed.')
            else :
                QtGui.QMessageBox.critical(self, 'Installation Failed', 'Failed to install this package.')
        
    def uninstallPackage(self):
        selection = None
        packageName = str(self.txtUninstallByName.text())
        
        if self.radioUninstallBySelection.isChecked() and self.lstPackageList.currentItem() :
            selection = str(self.lstPackageList.currentItem().text())
        
        elif self.radioUninstallByName.isChecked() :
            selection = packageName
        
        if selection :
            self.txtUninstallByName.clear()
            if self.packageManager.remove(selection) :
                QtGui.QMessageBox.information(self,'Succeeded','The Package was successfully uninstalled.')
            else :
                QtGui.QMessageBox.critical(self, 'Failed', 'Failed to uninstall this package.')
    
    def search(self):
        name = str(self.txtSearch.text())
        searchResult = self.packageManager.search(name)
        
        self.lstPackageList.clear()
        self.txtPackageInfo.clear()
        
        for package in searchResult :
            self.lstPackageList.addItem(package)
        
    
if __name__ == '__main__':
    app = QtGui.QApplication(argv)
    abstract_window = FramePackageManager()
    abstract_window.show()
    app.exec_()
